<?php

namespace App\Http\Controllers\admin;

use DB;
use App\Http\Controllers\Controller;

class ScriptController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdmin()
    {
        $info = DB::connection('iot') -> select('select * from sa_admin');
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['account'] = $val['account'];
            $data[$key]['pwd'] = $val['password'];
            $data[$key]['name'] = $val['linkman'];
            $data[$key]['phone'] = $val['phone'];
            $data[$key]['status'] = 1;
        }
        $res = DB::table('admin') -> insert($data);
        dd($res);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAgent()
    {
        $info = DB::connection('iot') -> select('select * from sa_agent');
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['name'] = $val['agent_name'];
            $data[$key]['pid'] = $val['pid'];
            $data[$key]['path'] = substr($val['path'],0,-1);
        }
        $res = DB::table('agent') -> insert($data);
        dd($res);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function getApprove()
    {
        $info = DB::connection('iot') -> select('select * from sa_approve');
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['agent_id'] = $val['agent_pid'];
            $data[$key]['name'] = $val['agent_name'];
            $data[$key]['account'] = $val['account'];
            $data[$key]['pwd'] = $val['password'];
            $data[$key]['linkman'] = $val['linkman'];
            $data[$key]['phone'] = $val['phone'];
            $data[$key]['status'] = $val['status'];

        }
        $res = DB::table('approve') -> insert($data);
        dd($res);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getArea()
    {
        $info = DB::connection('iot') -> select('select * from sa_area');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['a_id'];
            $data[$key]['name'] = $val['a_name'];
            $data[$key]['agent_id'] = $val['a_agent_id'];
        }
        $res = DB::table('area') -> insert($data);
        dd($res);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getAreamember()
    {
        $data = [];
        $info = DB::connection('iot')
            -> select('select * from sa_member_manager_area where mma_a_id != 0 AND mma_b_id = 0 AND mma_r_id = 0');
        //dd($info);
        foreach ($info as $key=>$val){
            if(!empty($val['mma_uid'])){
                $uid = explode(',',$val['mma_uid']);
                foreach ($uid as $k=>$v){
                    array_push($data,['area_id'=>$val['mma_a_id'],'member_id'=>$v]);
                }
            }
        }
        //dd($data);
        $res = DB::table('area_member') -> insert($data);
        dd($res);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getBuilding()
    {
        $info = DB::connection('iot') -> select('select * from sa_building');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['b_id'];
            $data[$key]['name'] = $val['b_name'];
            $data[$key]['latitude'] = $val['b_latitude'];
            $data[$key]['longitude'] = $val['b_longitude'];
            $data[$key]['area_id'] = $val['b_a_id'];
            $data[$key]['agent_id'] = $val['b_agent_id'];
        }
        $res = DB::table('building') -> insert($data);
        dd($res);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getBuildingmember()
    {
        $data = [];
        $info = DB::connection('iot')
            -> select('select * from sa_member_manager_area where mma_a_id != 0 AND mma_b_id != 0 AND mma_r_id = 0');
        //dd($info);
        foreach ($info as $key=>$val){
            if(!empty($val['mma_uid'])){
                $uid = explode(',',$val['mma_uid']);
                foreach ($uid as $k=>$v){
                    array_push($data,['building_id'=>$val['mma_b_id'],'member_id'=>$v]);
                }
            }

        }
        //dd($data);
        $res = DB::table('building_member') -> insert($data);
        dd($res);
    }

    public function getRoom(){
        $info = DB::connection('iot') -> select('select * from sa_room');
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['r_id'];
            $data[$key]['name'] = $val['r_name'];
            $data[$key]['building_id'] = $val['r_b_id'];
            $data[$key]['agent_id'] = $val['r_agent_id'];
        }
        $res = DB::table('room') -> insert($data);
        dd($res);
    }

    public function getRoommember()
    {
        $data = [];
        $info = DB::connection('iot')
            -> select('select * from sa_member_manager_area where mma_a_id != 0 AND mma_b_id != 0 AND mma_r_id != 0');
        foreach ($info as $key=>$val){
            if(!empty($val['mma_uid'])){
                $uid = explode(',',$val['mma_uid']);
                foreach ($uid as $k=>$v){
                    array_push($data,['room_id'=>$val['mma_r_id'],'member_id'=>$v]);
                }
            }
        }
        $res = DB::table('room_member') -> insert($data);
        dd($res);
    }

    public function getSmoke(){
        $info = DB::connection('iot') -> select('select * from sa_smoke_detector');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['num'] = $val['num'];
            $data[$key]['DevEUI'] = $val['eui'];
            $data[$key]['room_id'] = $val['r_id'];
            $data[$key]['loc'] = $val['location'];
            $data[$key]['model_id'] = $val['model_id'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['camera_id'] = $val['camera_id'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('smoke') -> insert($data);
        dd($res);
    }

    public function getElectric(){
        $info = DB::connection('iot') -> select('select * from sa_electric_detector');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['num'] = $val['num'];
            $data[$key]['DevEUI'] = $val['eui'];
            $data[$key]['room_id'] = $val['r_id'];
            $data[$key]['loc'] = $val['location'];
            $data[$key]['model_id'] = $val['model_id'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['camera_id'] = $val['camera_id'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('electric') -> insert($data);
        dd($res);
    }

    public function getGas(){
        $info = DB::connection('iot') -> select('select * from sa_gas_detector');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['num'] = $val['num'];
            $data[$key]['DevEUI'] = $val['eui'];
            $data[$key]['room_id'] = $val['r_id'];
            $data[$key]['loc'] = $val['location'];
            $data[$key]['model_id'] = $val['model_id'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['camera_id'] = $val['camera_id'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('gas') -> insert($data);
        dd($res);
    }

    public function getHelp(){
        $info = DB::connection('iot') -> select('select * from sa_call_help_detector');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['num'] = $val['num'];
            $data[$key]['DevEUI'] = $val['eui'];
            $data[$key]['room_id'] = $val['r_id'];
            $data[$key]['loc'] = $val['location'];
            $data[$key]['model_id'] = $val['model_id'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['camera_id'] = $val['camera_id'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('help') -> insert($data);
        dd($res);
    }

    public function getInfrared(){
        $info = DB::connection('iot') -> select('select * from sa_infrared_detector');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['num'] = $val['num'];
            $data[$key]['DevEUI'] = $val['eui'];
            $data[$key]['room_id'] = $val['r_id'];
            $data[$key]['loc'] = $val['location'];
            $data[$key]['model_id'] = $val['model_id'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['camera_id'] = $val['camera_id'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('infrared') -> insert($data);
        dd($res);
    }

    public function getGateway(){
        $info = DB::connection('iot') -> select('select * from sa_base_station');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['name'] = $val['bs_name'];
            $data[$key]['GatewayID'] = $val['bs_eui'];
            $data[$key]['power'] = $val['bs_txpower'];
            $data[$key]['room_id'] = $val['r_id'];
            $data[$key]['model_id'] = 68;
            $data[$key]['agent_id'] = $val['bs_agent_id'];
            $data[$key]['status'] = $val['bs_status'];
        }
        $res = DB::table('gateway') -> insert($data);
        dd($res);
    }

    public function getTest(){
        $info = DB::connection('qykj') -> select('select * from sa_smoke_detector');
        //dd($info);
        foreach ($info as $val){
            //查询日志表
            $temp = DB::connection('qykj') -> select('select lg_a_name,lg_b_name,lg_r_name,lg_agent_id from sa_log_msg where lg_eui = "'.$val['eui'].'" limit 1');
            if(!empty($temp)){
                var_dump($temp);
                //判断区域表是否已存在
                $singleArea = DB::connection('qykj') -> select('select * from sa_area where a_id = '.$val['a_id']);
                if(empty($singleArea)){
                    //插入区域表
                    DB::connection('qykj') -> insert('insert into sa_area (a_id, a_name, a_agent_id) values (?, ?, ?)', [$val['a_id'], $temp[0]['lg_a_name'], $temp[0]['lg_agent_id']]);
                }
                //判断关系表是否已存在
                $linkArea = DB::connection('qykj') -> select('select * from sa_member_manager_area where mma_a_id = '.$val['a_id'].' AND mma_b_id = 0 AND mma_r_id = 0');
                if(empty($linkArea)){
                    //插入关系表
                    DB::connection('qykj') -> insert('insert into sa_member_manager_area (mma_a_id, mma_b_id, mma_r_id) values (?, ?, ?)', [$val['a_id'], 0, 0]);
                }

                //判断建筑表是否已存在
                $singleBuilding = DB::connection('qykj') -> select('select * from sa_building where b_id = '.$val['b_id']);
                if(empty($singleBuilding)){
                    //插入区域表
                    DB::connection('qykj') -> insert('insert into sa_building (b_id, b_a_id, b_name, b_agent_id) values (?, ?, ?, ?)', [$val['b_id'], $val['a_id'], $temp[0]['lg_b_name'], $temp[0]['lg_agent_id']]);
                }
                //判断关系表是否已存在
                $linkArea = DB::connection('qykj') -> select('select * from sa_member_manager_area where mma_a_id = '.$val['a_id'].' AND mma_b_id = '.$val['b_id'].' AND mma_r_id = 0');
                if(empty($linkArea)){
                    //插入关系表
                    DB::connection('qykj') -> insert('insert into sa_member_manager_area (mma_a_id, mma_b_id, mma_r_id) values (?, ?, ?)', [$val['a_id'], $val['b_id'], 0]);
                }

                //判断房间表是否已存在
                $singleRoom = DB::connection('qykj') -> select('select * from sa_room where r_id = '.$val['r_id']);
                if(empty($singleRoom)){
                    //插入区域表
                    DB::connection('qykj') -> insert('insert into sa_room (r_id, r_a_id, r_b_id, r_name, r_agent_id) values (?, ?, ?, ?, ?)', [$val['r_id'], $val['a_id'], $val['b_id'], $temp[0]['lg_r_name'], $temp[0]['lg_agent_id']]);
                }
                //判断关系表是否已存在
                $linkArea = DB::connection('qykj') -> select('select * from sa_member_manager_area where mma_a_id = '.$val['a_id'].' AND mma_b_id = '.$val['b_id'].' AND mma_r_id = '.$val['r_id']);
                if(empty($linkArea)){
                    //插入关系表
                    DB::connection('qykj') -> insert('insert into sa_member_manager_area (mma_a_id, mma_b_id, mma_r_id) values (?, ?, ?)', [$val['a_id'], $val['b_id'], $val['r_id']]);
                }
            }
        }
    }

    public function getLa(){
        $info = DB::connection('iot') -> select('select b_id from sa_building WHERE b_agent_id = 4 AND b_a_id = 2');
        //dd($info);
        foreach ($info as $v){
            $temp = rand(0,1);
            if($temp == 0){
                $long = rand(115345323,115354212)/1000000;
                $lat = rand(33017756,33017968)/1000000;
            }else{
                $long = rand(115344982,115351153)/1000000;
                $lat = rand(33019103,33019262)/1000000;
            }
            //var_dump([$long,$lat]);
            DB::connection('iot') -> update('update sa_building set b_longitude = '.$long.', b_latitude = '.$lat.' where b_id = '.$v['b_id']);
        }
    }

    public function getMember(){
        $info = DB::connection('iot') -> select('select * from sa_member');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['id'] = $val['uid'];
            $data[$key]['name'] = $val['username'];
            $data[$key]['email'] = $val['email'];
            $data[$key]['wechat_openid'] = $val['wechat_openid'];
            $data[$key]['phone'] = $val['mobile'];
            $data[$key]['agent_id'] = $val['agent_id'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('member') -> insert($data);
        dd($res);
    }

    public function getDevice(){
        $info = DB::connection('iot') -> select('select * from sa_device');
        //dd($info);
        foreach($info as $key=>$val){
            $data[$key]['DevEUI'] = $val['DevEUI'];
            $data[$key]['DevAddr'] = $val['DevAddr'];
            $data[$key]['NwkSKey'] = $val['NwkSKey'];
            $data[$key]['AppSKey'] = $val['AppSKey'];
            $data[$key]['FCntUp'] = $val['FCntUp'];
            $data[$key]['FCntDn'] = $val['FCntDn'];
            $data[$key]['remark'] = $val['remark'];
            $data[$key]['status'] = $val['status'];
        }
        $res = DB::table('device') -> insert($data);
        dd($res);
    }

    public function getRole(){
        $info = DB::table('admin') -> get();
        //dd($info);
        foreach ($info as $v){
            $data = [
                'name'=>'总账号',
                'auth_ids'=>'0',
                'agent_id'=>$v['agent_id']
            ];
            $role_id = DB::table('role') -> insertGetId($data);
            DB::table('admin') -> where('id',$v['id']) -> update(['role_id'=>$role_id]);
            echo '1'."<br>";
        }
    }

    public function getHello(){
        $info = DB::table('auth') -> get();
        foreach ($info as $v){
            DB::table('auth') -> where('id',$v['id']) -> update(['module'=>substr($v['module'],3)]);
        }
    }
}
